 import axios from 'axios';
 import db from '../mysql/index.js';
 import qs from 'qs';
//  验证用户信息
 function uploadInfo(req,res) {
    let file = req.file
    let path = 'http://127.0.0.1:8080/img/'+file.filename
    res.send({
      status:200,
        file: path
    })
  }
  function loginn(req,res) {
    db.query('select * from userinfo where openid=?',req.query.openid,function (err,result) {
      if(result.length==1){
        res.send({
          status:200,
          msg:'登录成功',
          data:result[0]
        })
      }else{
        res.send({
          status:201,
          msg:'登录失败'
        })
        
      }
      })
    }
  function login(req,res) {
    axios({
      method:'get',
      url:'https://api.weixin.qq.com/sns/jscode2session',
      params:{
        appid:'wx46ed2cf7b729029f',
        secret:'53fdc2da35319c2f7962e4430bf3151e',
        js_code:req.query.code,
        grant_type:'authorization_code'
      }
    }).then(function (response) {
      db.query('select * from userinfo where openid=?',response.data.openid,function (err,result) {
        if(err){
          return res.send({
             status:201,
             msg:'操作失败'
           })
         }
         if(result.length==1){
           console.log(result[0]);
           res.send({
             status:200,
             msg:'登录成功',
             data:result[0]
           })
         }else{

          let userinfo=JSON.parse(req.query.userInfo)
      db.query('insert into userinfo(openid,nickName,userImg) values(?,?,?)',[response.data.openid,userinfo.nickName,userinfo.avatarUrl],function (err,result) { 
        if(err){
         return res.send({
            status:201,
            msg:'插入失败'
          })
        }
        res.send({
          status:200,
          msg:'插入成功',
          data:{
            openid:response.data.openid,
            nickName:userinfo.nickName,
            userImg:userinfo.avatarUrl,
          }
        })
       })
         }
      })
      
      
    })
    }
  function getMessage(req,res) {
    db.query(`select * from userinfo where openid='${req.query.data}'`,function (err,result) { 
      if(err){
        return res.send({
          status:201,
          msg:'查询失败',
          err:err
        })
      }
      if(result[0].verify===0){
        return res.send({
          status:201,
          msg:'该用户未认证'
        })
      }
      db.query('select * from message where sex=?',req.query.sex,function (err,result) {
        if(err){
          return res.send({
            status:201,
            msg:'获取失败'
          })
        }
        res.send({
          status:200,
          msg:'获取成功',
          data:result[Math.floor(Math.random()*result.length)]
        })
      })
     })
      
    }
  function postMessage(req,res) { 
    db.query(`select * from userinfo where openid='${JSON.parse(req.body.userInfo).openid}'`,function (err,result) { 
      if(err){
        return res.send({
          status:201,
          msg:'插入失败',
          err:err
        })
      }
      if(result[0].verify===0){
        return res.send({
          status:201,
          msg:'该用户未认证'
        })
      }
      let file = req.file
      let path = 'http://127.0.0.1:8080/contextImg/'+file.filename
      var message=req.body.message;
      
      db.query('insert into message(createTime,context,contextImg,sex,author) values(?,?,?,?,?)',[req.time,message,path,req.userInfo.sex,req.userInfo.nickName],function (err,result) { 
        if(err){
          return res.send({
            status:201,
            msg:'插入失败',
            err:err
          })
        }
        res.send({
          status:200,
          msg:'插入成功'
        })
       })
      
    })

    }
  export default{
    uploadInfo,
    login,
    loginn,
    getMessage,
    postMessage
  }
  